﻿using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoMapperSamples.Samples
{
    [TestClass]
    public class Flattening
    {
        public Flattening()
        {
        }

        [TestMethod]
        public void FlatteningTest()
        {
            Mapper.Initialize(cfg =>
                cfg.CreateMap<OrderModel, OrderViewModel>()
                .ForMember(dest=>dest.OrderNum,conf =>conf.MapFrom(src=>src.Num))
            );
            var srcOrder = new OrderModel
            {
                OrderNo="订单号1",
                Goods=new GoodsModel { Name="牛奶"},
                GetTotal=1000,
                Price=12.30M,
                Num=10000
            };
            OrderViewModel destOV = Mapper.Map<OrderModel, OrderViewModel>(srcOrder);

            Assert.AreEqual("订单号1",destOV.OrderNo);
            Assert.AreEqual("牛奶",destOV.GoodsName);
            Assert.AreEqual(1000,destOV.Total);
            Assert.AreEqual("12.30",destOV.price);
            Assert.AreEqual(10000,destOV.OrderNum);

        }

    }

    /*------------------------源对象----------------------------*/
    public class OrderModel
    {
        public string OrderNo{set;get;}

        public GoodsModel Goods{set;get;}

        public int GetTotal{set;get;}

        public decimal Price{set;get;}

        public int Num { set; get; } 
    }

    public class GoodsModel
    {
        public string Name { set; get; }    
    }

    /*------------------------目标对象----------------------------*/
    public class OrderViewModel
    {
        public string OrderNo{set;get;}

        public string GoodsName{set;get;}

        public int Total{set;get;}

        public string price{set;get;}

        public int OrderNum { set; get; } 
    } 
}
